package com.hlkj.pay.config;


/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

/**
 * @author HlpayTeam
 * @date 2025/05/08 15:40
 */
public class WrappersFactory {

    private final static List<String> ignoreList = new ArrayList<>();

    static {
        ignoreList.add("id");
        ignoreList.add("updateTime");
        ignoreList.add("createTime");
        ignoreList.add("createdBy");
        ignoreList.add("updatedBy");
        ignoreList.add("deleteFlag");
    }

    /**
     * 数据库更新 null LambdaUpdateWrapper封装
     * @param entity
     * @return
     * @param <T>
     */
    public static <T> LambdaUpdateWrapper<T> updateWithNullField(T entity) {
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
        List<Field> allFields = TableInfoHelper.getAllFields(entity.getClass());
        MetaObject metaObject = SystemMetaObject.forObject(entity);
        for (Field field : allFields) {
            if (!ignoreList.contains(field.getName())) {
                Object value = metaObject.getValue(field.getName());
                updateWrapper.set(StringUtils.camelToUnderline(field.getName()), value);
            }
        }
        return updateWrapper.lambda();
    }
}
